package com.novelot.plugin.allinone.tinify;

import com.tinify.Tinify;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class TinifyView extends JPanel {

    private DefaultListModel listModel = new DefaultListModel();

    public TinifyView() {
        Tinify.setKey("Qwp1YnY7b18B8jh2bbsw7V93n3BpPrfh");

        this.setLayout(new GridBagLayout());
        addListView();
        addBtnCompress();
    }


    private void addListView() {
        JList listView = new JList<>(listModel);
        listView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listView.setCellRenderer(new DefaultListCellRenderer());
        JScrollPane scrollPane = new JScrollPane(listView);

        MyDropTargetListener listener = new MyDropTargetListener(this);
        new DropTarget(scrollPane, DnDConstants.ACTION_COPY_OR_MOVE, listener, true);

        GridBagConstraints constraintsList = new GridBagConstraints();
        constraintsList.gridx = 1;
        constraintsList.gridy = 1;
        constraintsList.gridwidth = 1;
        constraintsList.gridheight = 1;
        constraintsList.weightx = 1;
        constraintsList.weighty = 1;
        constraintsList.fill = GridBagConstraints.BOTH;
        this.add(scrollPane, constraintsList);
    }

    private void addBtnCompress() {
        JButton btnCompress = new JButton();
        btnCompress.setText("压缩");
        btnCompress.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String pathFrom = "/Users/momo/Desktop/tinified/hani_icon_videodialog_close.png";
                String pathTo = "/Users/momo/Desktop/tinified/hani_icon_videodialog_close_tinified.png";
                TinifyUtils.compress(pathFrom, pathTo);
            }
        });

        GridBagConstraints constraintsCompress = new GridBagConstraints();
        constraintsCompress.gridx = 0;
        constraintsCompress.gridy = 2;
        constraintsCompress.gridwidth = 0;
        constraintsCompress.gridheight = 1;
        constraintsCompress.weightx = 0;
        this.add(btnCompress, constraintsCompress);
    }

    static class MyDropTargetListener implements DropTargetListener {

        private final TinifyView tinifyView;

        public MyDropTargetListener(TinifyView tinifyView) {
            this.tinifyView = tinifyView;
        }

        @Override
        public void dragEnter(DropTargetDragEvent dropTargetDragEvent) {

        }

        @Override
        public void dragOver(DropTargetDragEvent dropTargetDragEvent) {

        }

        @Override
        public void dropActionChanged(DropTargetDragEvent dropTargetDragEvent) {

        }

        @Override
        public void dragExit(DropTargetEvent dropTargetEvent) {

        }

        @Override
        public void drop(DropTargetDropEvent event) {
            if (event.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                event.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                try {
                    List<File> files = (List<File>) event.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                    if (files != null && !files.isEmpty()) {
                        for (File file : files) {
                            tinifyView.listModel.addElement(file.getAbsolutePath());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                event.dropComplete(true);
            }
        }
    }

}
